<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/page/common/commonConfig.jsp"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>

<head>
    <base href="<%=basePath%>">
    <title>单位用户列表</title>
    <script src="<%=basePath%>component/layout.js"></script>
    <script src="<%=basePath%>component/toolbar.js"></script>
    <script src="<%=basePath%>component/grid.js"></script>
    <script type="text/javascript" src="<%=basePath%>dwrsystem/interface/userService.js"></script> 
	<script type="text/javascript" src="<%=basePath%>dwrsystem/engine.js"></script>
	<script type="text/javascript">
	var mygrid;
	var myUnittree;
	var unitId;
	var willSelectedUserMainId;
	var addUserWindow;
	var dhxLayout = createLayoutNormal('1C',false,true);

	var dhxGrid = {
		initDataTable : function () {
		    mygrid.setHeader("序号,登录姓名,登录账号,备注");
		    mygrid.setInitWidths("50,150,150,*");
		    mygrid.setColAlign("right,left,left,left");
		    mygrid.setColSorting("int,str,str,str");
		    mygrid.setColTypes("ro,ro,ro,ro");
		    mygrid.attachEvent("onRowSelect",this.reloadParentSystemAndBusinessGrid);
		    mygrid.attachEvent("onRowDblClicked",this.openUser);
		    mygrid.attachEvent("onEnter",this.openUser);
		    mygrid.init();
		    setSkin(mygrid);
		    mygrid.loadXML('<%=basePath%>system-user/UserAction!userGridFromUnit.action?userForm.unitId=' + unitId,function(data){
		    	if(willSelectedUserMainId != null && willSelectedUserMainId != ''){
		    		mygrid.selectRowById(willSelectedUserMainId);
		    		parent.reloadSystemAndBusinessGrid(willSelectedUserMainId);
		    	}
		    });
		},
		reloadParentSystemAndBusinessGrid : function(){
			parent.reloadSystemAndBusinessGrid(mygrid.getSelectedId());
		},
		openUser : function(rowID,celInd){
			//waitingWindow();
			myUnittree.selectNode(rowID + "|" + unitId);
		},
		rowUp : function(){
			var rowId = mygrid.getSelectedId();
			if(rowId) {
				this.sort('up',rowId);
			}else{
				parent.parent.notifyWarn('请选择移动项');
			}
		},
		rowDown : function(){
			var rowId = mygrid.getSelectedId();
			if(rowId){
				this.sort('down',rowId);
			}else{
				parent.parent.notifyWarn('请选择移动项');
			}
		},
		sort : function (status, id){
	        var rInd = mygrid.getRowId((status=='up'?(mygrid.getRowIndex(id)-1):(mygrid.getRowIndex(id)+1)));
	        if(!rInd)	return ;
	        layoutProgressOn(dhxLayout);
			userService.selectTUserSortNumFromUnit(unitId, rInd, function(data){
				if(data != null){
					userService.sortUserInUnit(data, id, unitId);
					if(status == 'up'){
						mygrid.moveRowUp(id);
						mygrid.cellById(mygrid.getRowId(mygrid.getRowIndex(id)),0).setValue(mygrid.getRowIndex(id)+1);
						mygrid.cellById(mygrid.getRowId(mygrid.getRowIndex(id)+1),0).setValue(mygrid.getRowIndex(id)+2);
						myUnittree.moveUpStrict(id);
					} else {
						mygrid.moveRowDown(id);
						mygrid.cellById(mygrid.getRowId(mygrid.getRowIndex(id)-1),0).setValue(mygrid.getRowIndex(id));
						mygrid.cellById(mygrid.getRowId(mygrid.getRowIndex(id)),0).setValue(mygrid.getRowIndex(id)+1);
						myUnittree.moveDownStrict(id);
					}
					parent.parent.notifyCreater('排序成功');
					layoutProgressOff(dhxLayout);
				}else{
					parent.parent.notifyWarn('排序发生错误');
					layoutProgressOff(dhxLayout);
					return;
				}
			});
		},
		delOneFromUnit : function(){
			var rowId = mygrid.getSelectedId();
			if(rowId){
				parent.confirmInfo('从当前单位中，删除选中用户？',
					function(choose){
						if('ok' == choose){
							layoutProgressOn(dhxLayout);
							userService.deleteTUserUnit(rowId, unitId, function(data){
								if(data == true){
									parent.parent.notifyCreater('从单位中删除成功');
									layoutProgressOff(dhxLayout);
									myUnittree.delNode(rowId + "|" + unitId);
								}else{
									layoutProgressOff(dhxLayout);
									parent.parent.notifyWarn('删除用户异常');
								}
							});
						}else{
							return;
						}
					}
				);
			}else{
				parent.parent.notifyWarn('请选择删除项');
			}
		},
		delOneAll : function(){
			var rowId = mygrid.getSelectedId();
			if(rowId){
				parent.confirmInfo('从当前单位中，删除选中用户？',
					function(choose){
						if('ok' == choose){
							layoutProgressOn(dhxLayout);
							userService.deleteTUserAll(rowId, function(data){
								if(data == true){
									var lastrowIndex = mygrid.getRowIndex(rowId)-1;
									mygrid.deleteSelectedRows();
									mygrid.selectRow(lastrowIndex);
									
									for(var i=(lastrowIndex+1) ;i<(mygrid.getRowsNum());i++){
										mygrid.cells(mygrid.getRowId(''+i),0).setValue(i+1);
									}
									parent.parent.notifyCreater('删除成功');
									layoutProgressOff(dhxLayout);
									parent.deleteUserOnTree(rowId,unitId);
								}else{
									parent.parent.notifyWarn('删除用户异常');layoutProgressOff(dhxLayout);
								}
							});
						}else{
							return;
						}
					}
				);
			}else{
				parent.parent.notifyWarn('请选择删除项');
			}
		}

	};
	
	function addUser(){
		parent.addUserPage(unitId);
	}

	function queryUsers(){
		parent.openUsers(unitId,mygrid.getAllRowIds(),mygrid);
	}

	function configLayout(){
		var a = dhxLayout.cells('a');
		a.hideHeader();
		a.setText('主页面');
		a.fixSize(true, true);
		mygrid = createGridFromLayoutCell(a);
		if(power.getPower() != '' && (power.getPowerInsert() || power.getPowerDelete()))
			createToolbarFromDHXLayoutCell(a,function(toolbar){
			    toolbar.addText("info", 1, "操作:");
			    toolbar.addSeparator("sep1", 2);
			    if(power.getPowerInsert()){
			    	toolbar.addButton("add", 3, "增加新用户", "new.gif", "new_dis.gif");
			    	toolbar.addButton("addExits", 4, "增加已有用户", "add.png", "add.png");
			    }
			    if(power.getPowerDelete()){
			    	toolbar.addButton("delete", 5, "删除用户", "cut.gif", "cut_dis.gif");
			    	toolbar.addButton("deleteDeeper", 6, "彻底删除用户", "delete.png", "delete.png");
			    }
			    if(power.getPowerUpdate()){
			    	toolbar.addButton("updateUp", 7, "上移", "arrow_up.png", "arrow_up.png");
			    	toolbar.addButton("updateDown", 8, "下移", "arrow_down.png", "arrow_down.png");
			    }
			    toolbar.attachEvent("onClick", function(id) {
			    	if(id == 'add'){
			    		addUser();
			    	}
			    	if(id == 'addExits'){
			    		queryUsers();
			    	}
			    	if(id == 'delete'){
			    		dhxGrid.delOneFromUnit();
			    	}
			    	if(id == 'deleteDeeper'){
			    		dhxGrid.delOneAll();
			    	}
			    	if(id == 'updateUp'){
			    		dhxGrid.rowUp();
			    	}
			    	if(id == 'updateDown'){
			    		dhxGrid.rowDown();
			    	}
			    });
			});
	}
	
	$(function (){
		unitId = '${param.unitId}';
		willSelectedUserMainId = '${param.willSelectedUserMainId}';
		configLayout();
		dhxGrid.initDataTable();
	 	myUnittree = parent.window.MyTree;
	});
	</script>
</head>

<body>
</body>

</html>
